﻿local new_role_mode = Package:new("new_role_mode", Package.SpecialPack)
new_role_mode:addGameMode(require "packages/new_role_mode/new_role_mode")



local new_2v2 = Package:new("new_2v2", Package.SpecialPack)
new_2v2:addGameMode(require "packages/new_role_mode/new_2v2")


-- 木马已移动至官服“界限突破卡牌包”，任务完成

--[[
  木牛流马
  1.卸载木牛，辎牌进入弃牌堆 
  2.木牛移动到其他角色装备区，辎牌移动给其 
  3.木牛移动到处理区，辎牌暂存处理区
  4.木牛移出处理区时，将暂存的辎牌移动至目标装备区，没有目标置入弃牌堆
  5.巧变（装备区移动到装备区） 甘露（装备区移动到处理区再到装备区） 奇才（防止弃置）的正确互动
  6.甘露换牌，仅失去木马和得到木马的角色可见木马牌，由visiblePlayers心变智慧控制
--]]


--[[

local role_wooden_ox_trigger = fk.CreateTriggerSkill{
name = "#role_wooden_ox_trigger",
global = true,
mute = true,
refresh_events = {fk.AfterCardsMove},
can_refresh = function (self, event, target, player, data)
  return player == player.room.players[1]
end,
on_refresh = function(self, event, target, player, data)
  local room = player.room
  local moveInfos = {}
  for _, move in ipairs(data) do
    for _, info in ipairs(move.moveInfo) do
      if Fk:getCardById(info.cardId).name == "role_wooden_ox" then
        local from = move.from and room:getPlayerById(move.from)
        local to = move.to and room:getPlayerById(move.to)
        -- 处理木牛移出装备区
        if info.fromArea == Card.PlayerEquip and from and #from:getPile("role_carriage&") > 0 then
          local ids = from:getPile("role_carriage&")
          if move.toArea == Card.PlayerEquip and to and not to.dead then
            -- 若木马进入装备区，“辎”移动到他的私人牌堆
            table.insert(moveInfos, {
              -- 新建move需要写ids不需要写moveInfo，插入move需要写moveInfo不需要写ids
              ids = ids,
              moveInfo = table.map(ids, function(id)
                return {cardId = id,fromArea = Card.PlayerSpecial,fromSpecialName = "role_carriage&"}
              end),
              from = move.from,
              toArea = Card.PlayerSpecial,
              to = move.to,
              moveReason = fk.ReasonPut,
              skillName = self.name,
              specialName = "role_carriage&",
              moveVisible = false,
              visiblePlayers = {from.id, to.id},
            })
          elseif move.toArea == Card.Processing then
            -- 如果木马进入处理区，先把“辎”移动到处理区
            table.insert(moveInfos, {
              ids = ids,
              moveInfo = table.map(ids, function(id)
                return {cardId = id,fromArea = Card.PlayerSpecial,fromSpecialName = "role_carriage&"}
              end),
              from = move.from,
              toArea = Card.Processing,
              moveReason = fk.ReasonPut,
              skillName = self.name,
              moveVisible = false,
              visiblePlayers = {from.id},
            })
            room:setCardMark(Fk:getCardById(info.cardId), "role_wooden_ox_processing", {
              from = from.id, ids = ids
            })
          else
            -- 其他情况下，“辎”牌置入弃牌堆
            table.insert(moveInfos, {
              ids = ids,
              moveInfo = table.map(ids, function(id)
                return {cardId = id,fromArea = Card.PlayerSpecial,fromSpecialName = "role_carriage&"}
              end),
              from = move.from,
              toArea = Card.DiscardPile,
              moveReason = fk.ReasonPutIntoDiscardPile,
              skillName = self.name,
              moveVisible = true,
            })
          end
        end
        -- 处理木牛移出处理区
        local mark = Fk:getCardById(info.cardId):getMark("role_wooden_ox_processing")
        if info.fromArea == Card.Processing and type(mark) == "table" then
          local ids = table.filter(mark.ids, function (id)
            return room:getCardArea(id) == Card.Processing
          end)
          room:setCardMark(Fk:getCardById(info.cardId), "role_wooden_ox_processing", 0)
          if #ids > 0 then
            if move.toArea == Card.PlayerEquip and to and not to.dead then
              -- 若木马进入装备区，“辎”移动到他的私人牌堆
              table.insert(moveInfos, {
                ids = ids,
                moveInfo = table.map(ids, function(id)
                  return {cardId = id, fromArea = Card.Processing}
                end),
                toArea = Card.PlayerSpecial,
                to = move.to,
                moveReason = fk.ReasonPut,
                skillName = self.name,
                specialName = "role_carriage&",
                moveVisible = false,
                visiblePlayers = {mark.from, to.id},
              })
            else
              -- 其他情况下，“辎”牌置入弃牌堆
              table.insert(moveInfos, {
                ids = ids,
                moveInfo = table.map(ids, function(id)
                  return {cardId = id, fromArea = Card.Processing}
                end),
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonPutIntoDiscardPile,
                skillName = self.name,
                moveVisible = true,
              })
            end
          end
        end
      end
    end
  end
  if #moveInfos > 0 then
    local e = room.logic:getCurrentEvent():findParent(GameEvent.MoveCards, true)
    if e then
      -- 不要在refresh里执行移动操作，改为延时进行
      -- addExitFunc会被shutdown中断，addCleaner不会
      e:addExitFunc(function()
        room:moveCards(table.unpack(moveInfos))
      end)
    end
  end
end,
}
Fk:addSkill(role_wooden_ox_trigger)


local new_role_cards = Package:new("new_role_cards", Package.CardPack)

local role_wooden_ox_skill = fk.CreateActiveSkill{
  name = "role_wooden_ox_skill",
  attached_equip = "role_wooden_ox",
  prompt = "#role_wooden_ox-prompt",
  can_use = function(self, player, card)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isKongcheng() and #player:getPile("role_carriage&") < 5
  end,
  card_num = 1,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and table.contains(Self.player_cards[Player.Hand], to_select)
  end,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:addToPile("role_carriage&", effect.cards[1], false, self.name)
    if player.dead then return end
    local targets = table.filter(room:getOtherPlayers(player), function(p) return p:hasEmptyEquipSlot(Card.SubtypeTreasure) end)
    local ox = table.find(player:getCardIds("e"), function (id) return Fk:getCardById(id).name == "role_wooden_ox" end)
    if ox and #targets > 0 then
      local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#role_wooden_ox-move", self.name, true)
      if #tos > 0 then
        local to = room:getPlayerById(tos[1])
        room:moveCardTo(ox, Card.PlayerEquip, to, fk.ReasonPut, self.name, nil, true, player.id, nil)
      end
    end
  end,
}
Fk:addSkill(role_wooden_ox_skill)
local role_wooden_ox = fk.CreateTreasure{
  name = "role_wooden_ox",
  suit = Card.Diamond,
  number = 5,
  equip_skill = role_wooden_ox_skill,
}
new_role_cards:addCard(role_wooden_ox)



Fk:loadTranslationTable{
  ["role_wooden_ox"] = "木牛流马",
  [":role_wooden_ox"] = "装备牌·宝物<br/><b>宝物技能</b>：<br/>" ..
    "1. 出牌阶段限一次，你可将一张手牌置入仓廪（称为“辎”，“辎”数至多为5），然后你可将装备区里的【木牛流马】置入一名其他角色的装备区。<br/>" ..
    "2. 你可如手牌般使用或打出“辎”。<br/>" ..
    "3. 当你并非因交换而失去装备区里的【木牛流马】前，若目标区域不为其他角色的装备区，当你失去此牌后，你将所有“辎”置入弃牌堆。<br/>"..
    "<b>目前“辎”无法被转化使用</b>",
  ["role_wooden_ox_skill"] = "木牛",
  [":role_wooden_ox_skill"] = "出牌阶段限一次，你可将一张手牌置入仓廪（称为“辎”，“辎”至多有5张），然后你可将装备区里的【木牛流马】置入一名其他角色的装备区。你可如手牌般使用或打出“辎”。",
  ["#role_wooden_ox-move"] = "你可以将【木牛流马】移动至一名其他角色的装备区",
  ["role_carriage&"] = "辎",
  ["new_role_cards"] = "新身份模式卡牌",
  ["#role_wooden_ox_trigger"] = "木牛流马",
  ["#role_wooden_ox-prompt"] = "你可以将一张手牌扣置于【木牛流马】下",
}

--]]


















return {
  new_role_mode,
  --new_role_cards,
  new_2v2,
}


